﻿/*
 * ©2009-2010 XELF
 * http://xelf.info
 */

using System.Collections.Generic;

#if XNA || WINDOWS_PHONE
using Microsoft.Xna.Framework;
#endif

namespace XELF.Framework {

	/// <summary>
	/// 衝突判定用のポリゴンメッシュ
	/// </summary>
	public partial class BoundingMesh {
		/// <summary>
		/// 面群
		/// </summary>
		public List<BoundingFace> Faces = new List<BoundingFace>();

		/// <summary>
		/// 2点の線分上に障害物がないか確認する。
		/// </summary>
		/// <param name="from"></param>
		/// <param name="to"></param>
		/// <returns></returns>
		public bool IsVisible(Vector3 from, Vector3 to) {
			Vector3 d;
			Vector3.Subtract(ref to, ref from, out d);
			var dd = d.LengthSquared();
			if (dd == 0) return true;
			d.Normalize();
			var ray = new Ray(from, d);
			var rayLine = PluckerLine.CreateFromPointToPoint(from, to);
			foreach (var face in Faces) {
				float? distance;
				face.Intersects(ref ray, ref rayLine, out distance);
				if (distance != null) {
					if (distance < dd)
						return false;
				}
			}
			return true;
		}
	}

}
